package com.yuanfeng.base.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.base.entity.SmsInfoEntity;
import com.yuanfeng.base.mapper.SmsInfoMapper;
import com.yuanfeng.base.service.SmsInfoService;
import com.yuanfeng.commoms.config.YFConfig;
import com.yuanfeng.commoms.constant.CommonType;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.RedisUtil;
import com.yuanfeng.commoms.util.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
*
*/
@Service
public class SmsInfoServiceImpl extends ServiceImpl<SmsInfoMapper, SmsInfoEntity>
implements SmsInfoService {

    @Autowired
    private SmsInfoMapper smsInfoMapper;

    @Override
    public ResponseResult selectSMSInfoFeign() {
        QueryWrapper<SmsInfoEntity> queryWrapper = new QueryWrapper<>();
        SmsInfoEntity smsInfoEntity = new SmsInfoEntity();
        //从redis 中取值
        String smsInfoString = RedisUtil.getNoUpdateTime(YFConfig.SMS_CONFIG);
        if (StringUtils.isNotEmpty(smsInfoString)) {
            smsInfoEntity = JSONObject.parseObject(smsInfoString, SmsInfoEntity.class);
            return ResponseResult.success(CommonType.initializationStatus.SUCCESS.getCode(), BizCodeEnume.SMS_SUCCESS.getMsg(),smsInfoEntity);
        }

        //获取SMS配置信息
        smsInfoEntity = this.baseMapper.selectOne(queryWrapper);
        //返回给前端的标识 0 初始化成功但无配置信息  1已经存在配置信息并返回  2初始化失败
        Integer flag = 0;

        if(smsInfoEntity==null){
            int res=0;
            smsInfoEntity.setSmsStatus(CommonType.smsEnableDisableStatus.STARTUSING.getCode().toString());
            smsInfoEntity.setSmsIsactive(CommonType.pseudoDeleteIdentification.NORMAL.getCode().toString());
            //oss配置库没有配置信息就初始化数据
            res=this.baseMapper.insert(smsInfoEntity);
            if(res>0){
                int ossID=0;

                ossID=  this.baseMapper.getSMSbyId();

                return ResponseResult.success(CommonType.initializationStatus.SUCCESS.getCode(), CommonType.initializationStatus.SUCCESS.getDesc(),ossID);
            }else{

                return ResponseResult.fail(CommonType.initializationStatus.FAIL.getCode(), CommonType.initializationStatus.FAIL.getDesc());
            }
        } else {
            RedisUtil.set(YFConfig.SMS_CONFIG, JSON.toJSON(smsInfoEntity).toString());

            //oss配置库存在配置信息并返回给前端做展示
            return ResponseResult.success(CommonType.initializationStatus.SUCCESS.getCode(), BizCodeEnume.SMS_SUCCESS.getMsg(),smsInfoEntity);
        }
    }

    @Override
    public ResponseResult selectSMSInfoEncryption() {
        List<Map<String,Object>> list = new ArrayList<>();
        QueryWrapper<SmsInfoEntity> queryWrapper = new QueryWrapper<>();
        //接收返回信息
        Map<String,Object> result =new HashMap<>();
        SmsInfoEntity smsInfoEntity = this.baseMapper.selectOne(queryWrapper);
        if(null != smsInfoEntity ){
            Integer flag = 1;
            RedisUtil.set("SMS_CONFIG", JSON.toJSON(smsInfoEntity).toString());
            result.put("flag",flag);
            if(null != smsInfoEntity.getSmsGeneral()){
                Map<String,Object> template =new HashMap<>();
                template.put("key","通用模板");
                template.put("name","smsGeneral");
                template.put("value",smsInfoEntity.getSmsGeneral());
                template.put("desc","您的验证码是{1}，5分钟内有效。为了保护您的账号安全，验证短信请勿转发他人。如非本人操作，请忽略本短信。");
                list.add(template);
            }
            if(null != smsInfoEntity.getSmsInventory()){
                Map<String,Object> template =new HashMap<>();
                template.put("key","商品库存不足模板");
                template.put("name","smsInventory");
                template.put("value",smsInfoEntity.getSmsInventory());
                template.put("desc","您的商品{1}库存即将耗尽，请尽快补充调整。");
                list.add(template);
            }
            if(null != smsInfoEntity.getSmsShipments()){
                Map<String,Object> template =new HashMap<>();
                template.put("key","商家发货成功模板");
                template.put("name","smsShipments");
                template.put("value",smsInfoEntity.getSmsShipments());
                template.put("desc","您好，您在{1}店铺购买的商品商家已经发货，物流单号为{2}，物流公司是{3}，请确认收货。");
                list.add(template);
            }
            if(null != smsInfoEntity.getSmsPushTheDelivery()){
                Map<String,Object> template =new HashMap<>();
                template.put("key","提醒商家发货模板");
                template.put("name","smsPushTheDelivery");
                template.put("value",smsInfoEntity.getSmsPushTheDelivery());
                template.put("desc","发货提醒：{1}在您店铺购买的商品已超过{2}小时未发货，订单号为{3}，请尽快安排发货，如超过48小时未发货，平台将对您的商品进行下架处理");
                list.add(template);
            }
            if(null != smsInfoEntity.getSmsGoodsObligation()){
                Map<String,Object> template =new HashMap<>();
                template.put("key","提醒用户付款模板");
                template.put("name","smsGoodsObligation");
                template.put("value",smsInfoEntity.getSmsGoodsObligation());
                template.put("desc","商品待付款提醒：您在{1}店铺购买的{2}商品未支付，请尽快支付。");
                list.add(template);
            }
            if(null != smsInfoEntity.getSmsPlusMember()){
                Map<String,Object> template =new HashMap<>();
                template.put("key","PLUS权益到账提醒");
                template.put("name","smsPlusMember");
                template.put("value",smsInfoEntity.getSmsPlusMember());
                template.put("desc","远丰商城：尊敬的用户，您好！您已获得PLUS{1}权益，将于24小时内到达账户中心，您可登录网站查看www.yuanfeng.com");
                list.add(template);
            }
            if(null != smsInfoEntity.getSmsRetentionMoney()){
                Map<String,Object> template =new HashMap<>();
                template.put("key","质保金缴纳提醒");
                template.put("name","smsRetentionMoney");
                template.put("value",smsInfoEntity.getSmsRetentionMoney());
                template.put("desc","远丰商城：亲爱的卖家您好，根据平台质保金规则您需补缴{1}元质保金，请登录商家后台查询。");
                list.add(template);
            }
            if(null != smsInfoEntity.getSmsResidencyQualifications()){
                Map<String,Object> template =new HashMap<>();
                template.put("key","店铺入驻资质审核");
                template.put("name","smsResidencyQualifications");
                template.put("value",smsInfoEntity.getSmsResidencyQualifications());
                template.put("desc","亲爱的商家您好，您入驻的店铺{1}开店资质审核，{2}");
                list.add(template);
            }
            if(null != smsInfoEntity.getSmsShopPayRent()){
                Map<String,Object> template =new HashMap<>();
                template.put("key","店铺缴纳租金");
                template.put("name","smsShopPayRent");
                template.put("value",smsInfoEntity.getSmsShopPayRent());
                template.put("desc","亲爱的商家您好，{1}您提交的租金付款凭证{2}");
                list.add(template);
            }
            if(null != smsInfoEntity.getSmsEnterpriseAudit()){
                Map<String,Object> template =new HashMap<>();
                template.put("key","企业资质审核");
                template.put("name","smsEnterpriseAudit");
                template.put("value",smsInfoEntity.getSmsEnterpriseAudit());
                template.put("desc","企业审核结果通知：{1}您提交的企业信息审核{2}");
                list.add(template);
            }
            if(null != smsInfoEntity.getSmsDealerReview()){
                Map<String,Object> template =new HashMap<>();
                template.put("key","经销商审核");
                template.put("name","smsDealerReview");
                template.put("value",smsInfoEntity.getSmsDealerReview());
                template.put("desc","亲爱的商家您好，您申请成为{1}店铺的经销商审核被驳回，驳回原因: {2}");
                list.add(template);
            }
            result.put("templateList",list);
            result.put("smsInfoEntity",smsInfoEntity);
        }
        //oss配置库存在配置信息并返回给前端做展示
        return ResponseResult.success("SMS配置信息存在并返回成功",result);
    }
}
